package com.shun.oss.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.internal.OSSHeaders;
import com.aliyun.oss.model.*;
import com.shun.oss.common.OSS_CTL_TYPE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.URL;
import java.util.Date;

/**
 * 阿里云对象存储服务类：上传文件
 * @author clever
 * @since 2021-10-16
 */
@Slf4j
@Service
public class OssExecuteService extends OssBaseService {

    //1小时的秒数
    private final static int ONE_HOUR_SECONDS = 3600*1000;

    /**
     * 上传文件，通过文件
     * @return
     */
    public boolean uploadFile(String filePath, File file, OSS_CTL_TYPE ctlType) {
        log.debug("上传文件路径：{}",filePath);
        try {
            return uploadFile(filePath,new FileInputStream(file),ctlType);
        }catch (FileNotFoundException exception){
            log.error("不存在上传文件！");
            return false;
        }
    }

    /**
     * 上传文件，通过字节
     * @return
     */
    public boolean uploadFile(String filePath,byte[] bytes, OSS_CTL_TYPE ctlType){
        log.debug("上传文件路径：{}，文件大小：{}",filePath,bytes.length);
        return uploadFile(filePath,new ByteArrayInputStream(bytes),ctlType);
    }

    /**
     * 上传文件，通过流Stream
     * @return
     */
    public boolean uploadFile(String filePath, InputStream stream, OSS_CTL_TYPE ctlType){
        log.debug("上传文件路径：{}",filePath);
        OSS ossClient= getMgrOssClient();
        PutObjectRequest putObjectRequest = new PutObjectRequest(getBucketName(), filePath, stream);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        metadata.setObjectAcl(ctlType.getCtr());
        putObjectRequest.setMetadata(metadata);
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        closeOss(ossClient);
        return true;
    }

    /**
     * 获得下载文件
     * @param filePath
     * @param fileName
     * @return
     */
    public boolean downloadFile(String filePath,String saveFilePath,String fileName){
        log.debug("下载全");
        OSS ossClient= getMgrOssClient();
        ossClient.getObject(new GetObjectRequest(getBucketName(), filePath),
                new File(saveFilePath,fileName));
        closeOss(ossClient);
        return true;
    }

    /**
     * 获得下载的文件流
     * @param filePath
     * @param fileName
     * @return
     */
    public InputStream downloadStream(String filePath,String fileName){
        log.debug("下载字节流,filePath:{},源文件名称：{}",filePath,fileName);
        OSS ossClient= getMgrOssClient();
        OSSObject ossObject = ossClient.getObject(getBucketName(),filePath);
        closeOss(ossClient);
        return ossObject.getObjectContent();
    }

    /**
     * 获得资源文件的路径
     * @param filePath
     * @param ossType
     * @return
     */
    public String getOssFileUrl(String filePath,String ossType){
        if(ossType.equals("0")){
            OSS ossClient= getMgrOssClient();
            Date expiration = new Date(new Date().getTime() +  ONE_HOUR_SECONDS * getTimeout());
            URL url = ossClient.generatePresignedUrl(getBucketName(), filePath, expiration);
            return url.toString();
        }else{
            return getBucketUrl()+"/"+filePath;
        }
    }

    /**
     * 删除文件
     * @param filePath
     * @return
     */
    public boolean delOssFile(String filePath){
        OSS ossClient = getMgrOssClient();
        ossClient.deleteObject(getBucketName(),filePath);
        closeOss(ossClient);
        return true;
    }
}
